/**   
* @Title: PriceManagerServiceImpl.java 
* @Package com.kerrykidz.scminvoicing.service.impl 
* @Description: 用一句话描述该文件做什么 
* Copyright: Copyright (c) 2014 
* Company:wuwh team by iss
* @author: ScreenJxp   
* @date: 2014年8月26日 下午5:07:40 
* @version: V1.0
* update Release(文件修正记录)
* <pre>
* author--updateDate--description----------------------Flag————
* wuwh    2014-5-1    测试codesyle                      #wuwh001
*
*
*
* </pre>
*
*/
package com.kerrykidz.scminvoicing.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.scminvoicing.dao.mapper.GblScmPrdPriceEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.ScmPrdPriceEntityMapper;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdPriceEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmPrdPriceEntity;
import com.kerrykidz.scminvoicing.service.IPriceManagerService;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.UserInfo;
import com.kerrykidz.workflow.service.IWorkflowService;
import com.kerrykidz.workflow.vo.WorkflowVo;
import com.rdp.framework.exception.RDPException;
@Service
public class PriceManagerServiceImpl implements IPriceManagerService {
	//总部价格
	@Autowired
	private GblScmPrdPriceEntityMapper gblScmPrdPriceEntityMapper;
	//分店获取价格信息
	@Autowired
	private  ScmPrdPriceEntityMapper scmPrdPriceEntityMapper;
	
	@Autowired
	IWorkflowService workflowService;
	public List<GblScmPrdPriceEntity> findPriceInfoForHead(GblScmPrdPriceEntity entity) throws RDPException {
		List<GblScmPrdPriceEntity> resulList=gblScmPrdPriceEntityMapper.findPriceInfoForHead(entity);
		return resulList;
	}

	public List<ScmPrdPriceEntity> findPriceInfoForBranch(ScmPrdPriceEntity entity) throws RDPException {
		 List<ScmPrdPriceEntity> resultList=scmPrdPriceEntityMapper.findPriceInfoForBranch(entity);
		 UserInfo user=UserUtil.getUserFromSession();
		 //查询定价的流程数据
		 for(ScmPrdPriceEntity obj:resultList){
			 //草稿状态的数据
			 if("1".equalsIgnoreCase(obj.getStatus())){
				 	WorkflowVo work=new WorkflowVo();
				 	work.setIsCurrtOpt(user.getUserId().equalsIgnoreCase(obj.getApplyBy())?"1":"0");
				 	obj.setWorkflowVo(work);
			 }else if("2".equalsIgnoreCase(obj.getStatus())||"3".equalsIgnoreCase(obj.getStatus())||"4".equalsIgnoreCase(obj.getStatus())||"5".equalsIgnoreCase(obj.getStatus())){
				 	WorkflowVo work=workflowService.getWorkflowVo(obj.getFkFlowinstId(), obj.getStatus(), user);
					work.setOptHisVoList(workflowService.findOpinions(obj.getFkFlowinstId(),user));
					obj.setWorkflowVo(work);
			 }
		 }
		return resultList;
	}
  
	public void updatePriceForHead(GblScmPrdPriceEntity entity) throws RDPException {
			gblScmPrdPriceEntityMapper.updateEntity(entity);
	}

	public void savePriceForHead(GblScmPrdPriceEntity entity)
			throws RDPException {
			gblScmPrdPriceEntityMapper.saveEntity(entity);
	}
	public List<GblScmPrdPriceEntity> findHistoryForHead(String fkPrdId)
			throws RDPException {
			List<GblScmPrdPriceEntity> resultList=gblScmPrdPriceEntityMapper.findHistoryForHead(fkPrdId);
			return resultList;
	}
	public void savePriceForBranch(ScmPrdPriceEntity entity)
			throws RDPException {
			scmPrdPriceEntityMapper.savePriceForBranch(entity);
	}

	public void updatePriceForBranch(ScmPrdPriceEntity entity)
			throws RDPException {
			scmPrdPriceEntityMapper.updatePriceForBranch(entity);
			
	}
	public ScmPrdPriceEntity findPriceForBranchById(String pkPriceId,String userId)
			throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		ScmPrdPriceEntity priceEntity=scmPrdPriceEntityMapper.findPriceForBranchById(pkPriceId);
		if(priceEntity!=null){
			WorkflowVo work=workflowService.getWorkflowVo(priceEntity.getFkFlowinstId(), priceEntity.getStatus(), user);
			work.setOptHisVoList(workflowService.findOpinions(priceEntity.getFkFlowinstId(),user));
			priceEntity.setWorkflowVo(work);
		}
		return priceEntity;
	}
	public void deleteOldPrice(String fkPrdId,String fkBranId)
			throws RDPException {
		scmPrdPriceEntityMapper.deleteOldPrice(fkPrdId,fkBranId);
	}
	public List<ScmPrdPriceEntity> findHistoryForBranch(ScmPrdPriceEntity entity) throws RDPException {
		return scmPrdPriceEntityMapper.findHistoryForBranch(entity);
	}
	public void deleteTempPrice(String pkPriceId) throws RDPException {
		 scmPrdPriceEntityMapper.deleteTempPrice(pkPriceId);
	}
	public void createHistoryData(GblScmPrdPriceEntity entity) throws RDPException {
			gblScmPrdPriceEntityMapper.createHistoryData(entity);
	}
}
